package com.service.webservice.in;

import java.sql.Timestamp;
import java.util.Map;

import com.service.bean.Road_MixingPacket;
import com.service.bean.Road_MixingSignal;
import com.service.bean.Road_Vehicle;
import com.service.bean.Road_WayPara;
import com.service.bean.alarm.RoadAlarmInfo;
import com.service.bean.alarm.RoadAlarmRule;
import com.service.cache.CacheManager;
import com.service.other.FileConser;
import com.service.other.Logger;
import com.service.tools.JsonConvert;
import com.service.tools.Tools;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class RoadMixingServer {
		
	public void saveRoadMixingSignal(Road_MixingPacket packet) {
		if (null != packet.timeMillis && "" != packet.timeMillis) {
			packet.signal.mixingTime = new Timestamp(Long.parseLong(packet.timeMillis));
		}
		//收到消息
		FileConser.save(packet.signal.equipmentNO,packet.signal.toString(), "receive");
		
		// MD5验证
//		if(!packet.md5.equals(MD5Util.MD5by32w(packet.signal.toString()))){
//			Logger.err("Mixing ["+packet.signal.equipmentNO+"] process fail : md5 fail");
//			return;
//		}
//		
		Road_Vehicle vehicle = CacheManager.instance().roadMixingMap.get(packet.signal.equipmentNO);
		
		if(vehicle == null){
			 //Gps未绑定车辆
			Logger.err("Mixing ["+packet.signal.equipmentNO+"] process fail : vehicle no bind");
			return;
		}
		
		// 数据存入对应的信号管道
		packet.signal.companyCode = vehicle.company.code;
		packet.signal.mixingTemp2 = packet.signal.mixingTemp;
		sumMixingTotal(packet.signal);
		Map<String, String> map = CacheManager.instance().Mxformula.get(vehicle.company.code);
		if(packet.signal.formula != 0){
			if(packet.signal.formulaName == null || packet.signal.formulaName == ""){
				packet.signal.formulaName = map.get(String.valueOf(packet.signal.formula));
			}
		}else{
			if(packet.signal.formulaName != null && packet.signal.formulaName != ""){
				packet.signal.formula = Integer.valueOf(map.get(packet.signal.formulaName));
			}
		}
		Road_WayPara para = CacheManager.instance().mixWayPara.get(vehicle.company.code);
		if(para != null && packet.signal.mixingTemp < para.minValue){
			packet.signal.mixingTemp2 = Tools.getRandom(para.minValue, para.maxValue);
		}
		CacheManager.instance().getGpsSaveQueue_Mixing(vehicle.company).offer(packet.signal);
		calculateMiXingAlarm(vehicle,packet.signal);
		System.out.println(packet.signal.toString());
		
		return;
	}
	
	public void calculateMiXingAlarm(Road_Vehicle vehicle, Road_MixingSignal signal) {
		RoadAlarmInfo ai = null;
		for (RoadAlarmRule rule : vehicle.mixingAlarmRuleMap.values()) {
			try {
				if (rule.FormulaNo != 0 && rule.FormulaNo != signal.formula) {
					continue;
				}
				ai = rule.doMiXingAlarm(vehicle, signal);
				if (null != ai) {
					CacheManager.instance().getRoadAlarmSaveQueue(vehicle.company).offer(ai);// 待保存
					// NoticeMessageTool.checkMiXingSendMessag(vehicle,ai.AlarmType, signal, ai);
				}
			} catch (Exception e) {
				Logger.err(e);
			}
		}
	}
	
	/**
	 * 拌合楼WBS服务，接收json格式数据
	 * @param packetjson
	 */
	public void roadMixingSignalJson(String packetjson) {
		if (packetjson == null || packetjson.length() == 0) {
			return;
		}
		try {
			JSONArray jsonArray = JSONArray.fromObject(packetjson);
			for (int i = 0; i < jsonArray.size(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				Object obj = JsonConvert.fromJsonToObject(jsonObject, Road_MixingSignal.class);
				Road_MixingSignal signal = (Road_MixingSignal) obj;
				FileConser.save(signal.equipmentNO, signal.toString(), "receive");
				Road_Vehicle vehicle = CacheManager.instance().roadMixingMap.get(signal.equipmentNO);
				if (vehicle == null) {
					// Gps未绑定车辆
					Logger.err("Mixing [" + signal.equipmentNO + "] process fail: vehicle no bind");
					continue;
				}
				signal.companyCode = vehicle.company.code;
				signal.mixingTemp2 = signal.mixingTemp;
				sumMixingTotal(signal);
				Map<String, String> map = CacheManager.instance().Mxformula.get(vehicle.company.code);
				if(null!=map){
					if(signal.formula != 0){
						if(signal.formulaName == null || signal.formulaName == ""){
							signal.formulaName = map.get(String.valueOf(signal.formula));
						}
					}else{
						if(signal.formulaName != null && signal.formulaName != ""&& null !=map.get(signal.formulaName)){
							signal.formula = Integer.valueOf(map.get(signal.formulaName));
						}
					}
				}
				Road_WayPara para = CacheManager.instance().mixWayPara.get(vehicle.company.code);
				if(para != null && signal.mixingTemp < para.minValue){
					signal.mixingTemp2 = Tools.getRandom(para.minValue, para.maxValue);
				}
				CacheManager.instance().getGpsSaveQueue_Mixing(vehicle.company).offer(signal);
				calculateMiXingAlarm(vehicle,signal);
				System.out.println(signal.toString());
			}
		} catch (Exception e) {
			Logger.err("roadMixingSignal error:" + e.getMessage()+";packetjson="+packetjson);
		}
		return;
	}

	/**
	 * 每锅拌量计算
	 * @param packet
	 */
	public void sumMixingTotal(Road_MixingSignal signal) {
		//如果每锅拌量 = 0，进行计算你处理
		if(signal.mixingTotal == 0){			
			signal.mixingTotal = 
					signal.stone1+		//石料1
					signal.stone2+		//石料2
					signal.stone3+		//石料3
					signal.stone4+		//石料4
					signal.stone5+		//石料5
					signal.stone6+		//石料6
					signal.asphalt+		//沥青
					signal.powderLot1+	//粉料1
					signal.powderLot2+	//粉料2
					signal.thickPowder+	//粗料
					signal.thinPowder+	//细料
					signal.additive+	//添加剂
					signal.stone7+		//石料7(AM01旁通)
					signal.asphalt2+	//改性沥青(AB04)
					signal.asphalt3+	//改性沥青(AB01)
					signal.asphalt4;	//改性沥青(AB02)
			return;
		}
	}
	
	
}
